var fs = wx.getFileSystemManager();
var util = require('util');
var access_token;
var baidu_host = 'https://aip.baidubce.com';
var grant_type = 'client_credentials';
var client_id = 'H41RtbfwFW7RGUehXZQvlgE1';
var client_secret = 'rAszhATDmxxoWe2h2XWWe1tRtM7EnfbZ';
var testPath = 'F:/workspace/WechatProject/weixinxiaochengxu/images/color/colorDemo.png';

let get_access_token = () => {
  //发送接口请求
  wx.request({
    url: baidu_host + '/oauth/2.0/token?',
    method:'POST',
    header: {
      'content-type' : 'application/x-www-form-urlencoded',
    },
    data: {
      'grant_type' : grant_type,
      'client_id': client_id,
      'client_secret': client_secret,
    },
    success: function (res) {
      console.log(res.data.access_token),
      access_token = res.data.access_token
    },
    fail: function (err) {
      console.log(err.data)
    }
  })
}

/**
 * 黑白图片上色请求
 **/
let colourize = (base64image) => {
  //发送接口请求
  wx.request({
    url: baidu_host + '/rest/2.0/image-process/v1/colourize?access_token=' + access_token,
    method:'POST',
    header: {
      'content-type' : 'application/x-www-form-urlencoded',
    },
    data: {
      'image' : base64image,
    },
    success: function (res) {
      console.log(res.data)
    },
    fail: function (err) {
      console.log(err.data)
    }
  })
}

/**
 * 图片url转base64
 **/
let imageUrlToBase64 = (filePath) => {
  const imageData = util.promisify(fs.readFileSync({filePath:'testPath'})); // 例：xxx/xx/xx.png
  const imageBase64 = imageData.toString("base64");
  const imagePrefix = "data:image/png;base64,";
  console.log(imagePrefix + imageBase64);
}


//暴露出去的接口
module.exports = {
  get_access_token : get_access_token,
  colourize : colourize,
  imageUrlToBase64 : imageUrlToBase64,
}